package app.Disthanitian.thaistyle.EmerMez;

import android.app.Activity;
import java.util.ArrayList;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class ContactTest extends Activity{


	    ListView lvItem;
	    private Button btnAdd;
	    String displayName="", emailAddress="", phoneNumber="";
	    ArrayList<String> contactlist=new ArrayList<String>();
	    ArrayAdapter<String> itemAdapter;


	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.addcontactlist);
	       lvItem = (ListView)this.findViewById(R.id.About);  
	       btnAdd = (Button)this.findViewById(R.id.btn_addcontact);
	       itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
	       lvItem.setAdapter(itemAdapter);
	       btnAdd.setOnClickListener(new View.OnClickListener() {
	           public void onClick(View v) {
	               readContacts();
	           }
	       });
	    }

	    private void readContacts()
	    {
	        ContentResolver cr =getContentResolver();
	        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
	        while (cursor.moveToNext()) 
	        {
	            //displayName="";emailAddress=""; phoneNumber="";
	            displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
	            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
	            Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
	            while (emails.moveToNext()) 
	            { 
	                emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
	                break;
	            }
	            emails.close();
	            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
	            {
	                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
	                while (pCur.moveToNext()) 
	                {
	                     phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
	                    break;
	                }
	                pCur.close();
	            }
	                contactlist.add("DisplayName: "+displayName+", PhoneNumber: "+phoneNumber+", EmailAddress: "+ emailAddress+"\n");
	                itemAdapter.notifyDataSetChanged();
	        }
	        cursor.close(); 
	    }
	}

